<?php

class login extends IgnitedRecord
{
	var	$has_one = array('student', 'teacher', 'parent');
	 
	function login()
	{
        parent::IgnitedRecord();
    }
    
    function check_valid_pass($log, $pwd)
    {
        $this->db->where(array('login' => $log, 'passwd' => $pwd));
        $q = $this->db->get('logins');
        foreach ($q->result() as $row):
            return $row->id;
        endforeach;
        
        return NULL;
    }
    
    function check_user_type($id)
    {
        $result = array();
        
        $this->db->where('login_id', $id);
        $q = $this->db->get('parentts');
        foreach ($q->result() as $r):
            $result['firstname'] = $r->firstname;
            $result['secondname'] = $r->secondname;
            $result['type'] = 'parentt';
            return $result;
        endforeach;
        
        $this->db->where('login_id', $id);
        $q = $this->db->get('students');
        foreach ($q->result() as $r):
            $result['firstname'] = $r->firstname;
            $result['secondname'] = $r->secondname;
            $result['type'] = 'student';
            return $result;
        endforeach;

        $this->db->where('login_id', $id);
        $q = $this->db->get('teachers');
        foreach ($q->result() as $r):
            $result['firstname'] = $r->firstname;
            $result['secondname'] = $r->secondname;
            $result['type'] = 'teacher';
            return $result;
        endforeach;
        
        $this->db->where('login_id', $id);
        $q = $this->db->get('administrators');
        foreach ($q->result() as $r):
            $result['type'] = 'admin';
            return $result;
        endforeach;
        
        return NULL;
    }
    
    function change_pass($id, $pwd)
    {
        $this->db->where('id', $id);
        $this->db->update('logins', array('passwd' => $pwd));
    }
    
    function check_valid_email($log, $eml)
    {
        $this->db->where(array('login' => $log));
        $q = $this->db->get('logins');
        foreach ($q->result() as $row):
            $id = $row->id;
        endforeach;
        
        $this->db->where('login_id', $id);
        $q = $this->db->get('parentts');
        foreach ($q->result() as $r):
            if ($r->email == $eml):
                return True;
            endif;
        endforeach;
        
        $this->db->where('login_id', $id);
        $q = $this->db->get('students');
        foreach ($q->result() as $r):
            if ($r->email == $eml):
                return True;
            endif;
        endforeach;

        $this->db->where('login_id', $id);
        $q = $this->db->get('teachers');
        foreach ($q->result() as $r):
            if ($r->email == $eml):
                return True;
            endif;
        endforeach;
        
        return False;
    }
    
    function login_available($log)
    {
        $this->db->where('login', $log);
        $q = $this->db->get('logins');
        foreach ($q->result() as $r):
            return False;
        endforeach;
        
        return True;
    }
}
?>
